/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.click.app;

import com.click.app.model.CutDetail;
import com.click.app.model.LabelValue;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
 *
 * @author ronald
 */
public class Functions {
    public static List<String> getLabelFromLabelValue(List<LabelValue> list){
        List<String> labels = new ArrayList<String>();
        for(LabelValue lv : list)
            labels.add("["+ lv.getValue() +"] "+lv.getLabel());
        return labels;
    }
    public static Long[] getValueFromLabel(List<String> list){
        Long []values = new Long[list.size()];
        int i=0;
        for(String l : list){
            String value[] = l.split("]");
            String numString = value[0].substring(1,value[0].length());
            values[i] = new Long(numString);
            i++;
        }
        return values;
    }
    public static Map getHashMapFromLabelValue(List<LabelValue> list){
        HashMap<String, String> result = new HashMap<String,String>();
        for(LabelValue lv : list){
            result.put(lv.getValue(),lv.getLabel());
        }
        return result;
    }
    public static Long[] getArrayLongFromArrayObjects(Object []array){
        Long []result = new Long[array.length];
        for(int i=0 ; i<array.length ; i++){
            try{
                result[i] = new Long(array[i].toString());
            }catch(Exception e){ }
        }
        return result;
    }
    public static String generateFilter( List<String> names, List<String> operatorNames
                                       , List<String> values, List<String> operatorValues
                                       , String name, String value, List<String> joinsFields ){
        StringBuffer result = new StringBuffer();

        for (Iterator<String> itNames = names.iterator() , itOpNames = operatorNames.iterator()
            , itValues = values.iterator() , itOpValues = operatorValues.iterator()
            , itJoinsField = joinsFields.iterator() ; itNames.hasNext();) {
            String nameX = itNames.next();
            String opNameX = itOpNames.next();
            String valueX = itValues.next();
            String opValueX = itOpValues.next();

            result.append(" ( ");
            result.append(name);
            result.append(opNameX);
            result.append("'"+nameX+"'");
            result.append(" AND ");
            result.append(value);
            result.append(opValueX);
            result.append("'"+valueX+"'");
            result.append(" ) ");

            if(itNames.hasNext()){
                String joinField = itJoinsField.next();
                result.append(" "+joinField+" ");
            }   
        }
        return result.toString();
    }
    public static Double getDouble3Decimal(Double d){
        if(d != null){
            DecimalFormat threeDForm = new DecimalFormat("#.###");
            return Double.valueOf(threeDForm.format(d));
        }
        return d;
    }
}
